package ru.autofan.secure;

public enum AuthRoleSupport {

	ADMINISTRATOR("ADMINISTRATOR"),

	USER("USER"),

	ANONYMOUS("ANONYMOUS");

	private String type;

	public Boolean isApplicable(AuthRoleSupport anotherRole) {
		if (type.toUpperCase().equals("ADMINISTRATOR")) {
			return isAdministrator(anotherRole);
		} else if (type.toUpperCase().equals("USER")) {
			return isAdministratorOrUser(anotherRole);
		} else if (type.toUpperCase().equals("ANONYMOUS")) {
			return isAdministratorOrUserOrAnonymous(anotherRole);
		} else {
			throw new IllegalStateException("UNKNOW AuthRoleSupport!");
		}
	}

	private AuthRoleSupport(String type) {
		this.type = type;
	}

	private Boolean isAdministrator(AuthRoleSupport type) {
		return type.equals(AuthRoleSupport.ADMINISTRATOR);
	}

	private Boolean isAdministratorOrUser(AuthRoleSupport type) {
		return isAdministrator(type) || type.equals(AuthRoleSupport.USER);
	}

	private Boolean isAdministratorOrUserOrAnonymous(AuthRoleSupport type) {
		return isAdministratorOrUser(type)
				|| type.equals(AuthRoleSupport.ANONYMOUS);
	}
}
